﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using VisionApp.Entity;
using VisionApp.Interface;
using VisionAppManager.Model;
using System.Windows.Media.Imaging;
using CommonServiceLocator;
using GalaSoft.MvvmLight.Command;
using System.Configuration;
using OpenCvSharp;
using System.IO;
using System.Windows.Forms;

namespace VisionAppManager.ViewModel
{
    public class VMImgViewer : ViewModelBase
    {
        public RelayCommand RunCommand { get; private set; }
        public RelayCommand StopCommand { get; private set; }

        private Worker _worker;
        public Worker Worker
        {
            get
            {
                return _worker;
            }
            set
            {
                _worker = value;
                RaisePropertyChanged("Worker");
            }
        }
        public VMImgViewer(IWorker worker)
        {
            Worker = (Worker)worker;
            //Messenger.Default.Register<HImageContainer>(this, (rc) => updateView1(rc));
            //Messenger.Default.Register<VSResultContainer>(this, (rc) => updateView(rc));
            RunCommand = new RelayCommand(async () => await Worker.RunRecordProc1());
            StopCommand = new RelayCommand(async () => await Worker.StopRecordProc1());
        }
        //private void updateView(VSResultContainer rc)
        //{
        //    switch (rc.ID)
        //    {
        //        case ImagePosID.LEFTCAM:
        //            Im1.Dispatcher.Invoke(
        //                   new Action(
        //                        delegate
        //                        {
        //                            Im1.Source = (BitmapImage)rc.Image;
        //                            //Im1.Source.Freeze();
        //                        }
        //                   )
        //             );
        //            break;
        //        case ImagePosID.RIGHTCAM:
        //            Im2.Dispatcher.Invoke(
        //                   new Action(
        //                        delegate
        //                        {
        //                            Im2.Source = (BitmapImage)rc.Image;
        //                            //Im2.Source.Freeze();
        //                        }
        //                   )
        //             );
        //            break;
        //        default:
        //            Im1.Dispatcher.Invoke(
        //                   new Action(
        //                        delegate
        //                        {
        //                            Im1.Source = (BitmapImage)rc.Image;
        //                            //Im1.Source.Freeze();
        //                        }
        //                   )
        //             );
        //            Im2.Dispatcher.Invoke(
        //                   new Action(
        //                        delegate
        //                        {
        //                            Im2.Source = (BitmapImage)rc.Image;
        //                            //Im2.Source.Freeze();
        //                        }
        //                   )
        //             );
        //            break;
        //    }
        //}
        //private void updateView1(HImageContainer rc)
        //{
        //    Mat image = new Mat(rc.TextHeight, rc.TextWidth, MatType.CV_8UC1, rc.ImGray);
        //    switch (rc.ID)
        //    {
        //        case ImagePosID.LEFTCAM:
        //            Im1.Dispatcher.Invoke(
        //                   new Action(
        //                        delegate
        //                        {
        //                            Im1.Source = matToBitmapImage(image);
        //                        }
        //                   )
        //             );
        //            break;
        //        case ImagePosID.RIGHTCAM:
        //            Im2.Dispatcher.Invoke(
        //                   new Action(
        //                        delegate
        //                        {
        //                            Im2.Source = matToBitmapImage(image);
        //                        }
        //                   )
        //            );
        //            break;
        //        default:
        //            break;
        //    }
        //}
        //private BitmapImage matToBitmapImage(Mat image)
        //{
        //    System.Drawing.Bitmap bitmap = matToBitmap(image);
        //    using (MemoryStream stream = new MemoryStream())
        //    {
        //        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); // 坑点：格式选Bmp时，不带透明度
        //        stream.Position = 0;
        //        BitmapImage result = new BitmapImage();
        //        result.BeginInit();
        //        // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
        //        // Force the bitmap to load right now so we can dispose the stream.
        //        result.CacheOption = BitmapCacheOption.OnLoad;
        //        result.StreamSource = stream;
        //        result.EndInit();
        //        result.Freeze();
        //        return result;
        //    }
        //}
        //private System.Drawing.Bitmap matToBitmap(Mat image)
        //{
        //    return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
        //}
    }
}
